package com.example.prison.mapper;

import com.example.prison.entity.UseCarManagement;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author jiang
* @description 针对表【use_car_management】的数据库操作Mapper
* @createDate 2024-06-03 14:14:12
* @Entity com.example.prison.entity.UseCarManagement
*/
@Mapper
public interface UseCarManagementMapper extends BaseMapper<UseCarManagement> {

//    @Select("select * from use_car_management order by id desc")
    @Select("<script>select * from use_car_management " +
            " <where> " +
            " <if test=\"carNumber != null and carNumber != ''\">and car_number like concat('%',#{carNumber},'%') </if> " +
            " <if test=\"usePeople != null and usePeople != ''\">and use_people like concat('%',#{usePeople},'%') </if> " +
            " <if test=\"status != null and status != ''\">and status like concat('%',#{status},'%') </if> " +
            " <if test=\"useRange != null and useRange != ''\">and use_range like concat('%',#{useRange},'%') </if> " +
            " <if test=\"useStatus != null and useStatus != ''\">and use_status like concat('%',#{useStatus},'%') </if> " +
            " <if test=\"beginTime != null and beginTime != ''\">and end_time &gt;= #{beginTime} </if> " +
            " <if test=\"endTime != null and endTime != ''\">and use_time &lt;= #{endTime} </if> " +
            " </where> " +
            "order by id desc</script>")
    List<UseCarManagement> get(String carNumber, String usePeople, String status, String useRange, String useStatus, String beginTime, String endTime);

    @Insert("insert into use_car_management(car_number, use_people, use_time, end_time, status, use_reason,use_range,use_status,depart) " +
            "values (#{carNumber}, #{usePeople}, #{useTime}, #{endTime}, #{status}, #{useReason}, #{useRange}, #{useStatus},#{depart})")
    int add(UseCarManagement useCar);

    @Delete("<script>delete from use_car_management where id in <foreach collection=\"ids\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item} </foreach> </script>")
    void deleteUseCar(@Param("ids") Integer[] ids);

//    @Update("update use_car_management set car_number=#{useCar.carNumber},use_people=#{useCar.usePeople},use_time=#{useCar.useTime}," +
//            "end_time=#{useCar.endTime},status=#{useCar.status},use_reason=#{useCar.useReason},use_range=#{useCar.useRange}," +
//            "use_status=#{useCar.useStatus},depart=#{useCar.depart} where id = #{id}")
    @Update({"<script> UPDATE use_car_management",
            "<set>",
            "<if test='useCar.carNumber != null'>car_number = #{useCar.carNumber},</if>",
            "<if test='useCar.usePeople != null'>use_people = #{useCar.usePeople},</if>",
            "<if test='useCar.useTime != null'>use_time = #{useCar.useTime},</if>",
            "<if test='useCar.endTime != null'>end_time = #{useCar.endTime},</if>",
            "<if test='useCar.status != null'>status = #{useCar.status},</if>",
            "<if test='useCar.useReason != null'>use_reason = #{useCar.useReason},</if>",
            "<if test='useCar.useRange != null'>use_range = #{useCar.useRange},</if>",
            "<if test='useCar.useStatus != null'>use_status = #{useCar.useStatus},</if>",
            "<if test='useCar.depart != null'>depart = #{useCar.depart},</if>",
            "</set>",
            "where id = #{id} </script>"})
    int updateUseCar(UseCarManagement useCar,Integer id);

    @Select("select * from use_car_management where id=#{id}")
    UseCarManagement getById(@Param("id") Integer id);

    @Select("select * from use_car_management where car_number=#{carNumber}")
    List<UseCarManagement> getOneByNumber(@Param("carNumber") String carNumber);

    /** 获取使用车辆列表 */
    @Select("select * from use_car_management where end_time < #{date} and use_status='用车中' order by id desc")
    List<UseCarManagement> getNoReturn(@Param("date") String date);

    /** 获取使用车辆列表 */
    @Select("select * from use_car_management where use_status='用车中' order by id desc")
    List<UseCarManagement> getUsingLongCar();

    @Select("select * from use_car_management where car_number=#{carNumber} and use_status=#{status} and status='已审批' order by use_time desc LIMIT 1")
    UseCarManagement getOneByNumberAndUseStatus(@Param("carNumber")String carNumber, @Param("status")String status);

}




